package test.yzx.pproject.act3;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;

import test.yzx.pproject.R;
import test.yzx.pproject.testView.TestViewPager;
import test.yzx.pproject.testView.TopPartScrollView;

public class StickyHeaderViewPagerActivity extends AppCompatActivity {

    private TestViewPager viewPager;
    private TopPartScrollView scrollView;
    private int scrollViewY;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_sticky_header);
        viewPager = (TestViewPager) findViewById(R.id.viewPager);
        scrollView = (TopPartScrollView) findViewById(R.id.scrollView);


        final PagerFragment[] fs = {
            new PagerFragment(),new PagerFragment(),new PagerFragment(),new PagerFragment(),
        };
        viewPager.setFragments(fs);
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fs[position];
            }

            @Override
            public int getCount() {
                return 4;
            }
        });
        viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            public void onPageScrollStateChanged(int state) {
                if(state==ViewPager.SCROLL_STATE_IDLE){
                    scrollView.setEnabled(false);
                    scrollView.post(new Runnable() {
                        @Override
                        public void run() {
                            scrollView.setEnabled(true);
                        }
                    });
                }
            }
            public void onPageSelected(int position) {
                scrollViewY = scrollView.getScrollY();
            }
        });

    }


}
